<template>
    <div class="register-form">
        <div class="inner">
            <el-form label-width="8rem" ref="formDom" :model="state.form" :rules="state.rules">
                <el-form-item label="" label-width="0">
                    <el-row>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="开启" prop="ConnectEnable">
                                <el-checkbox size="default" v-model="state.form.ConnectEnable">开启
                                    <el-tooltip class="box-item" effect="dark" content="允许所有账号使用端口转发穿透，包括匿名，不允许则只有权限时可使用" placement="top">
                                        <el-icon>
                                            <Warning />
                                        </el-icon>
                                    </el-tooltip>
                                </el-checkbox>
                            </el-form-item>
                        </el-col>
                        <high-config>
                            <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                                <el-form-item label="bufsize" prop="BufferSize">
                                    <el-select size="default" v-model="state.form.BufferSize" placeholder="选择合适的buff">
                                        <el-option v-for="(item,index) in shareData.bufferSizes" :key="index" :label="item" :value="index"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </high-config>
                    </el-row>
                </el-form-item>
                <el-form-item label="" label-width="0">
                    <el-row>
                        <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                            <el-form-item label="短链接端口" prop="WebListens">
                                <el-input size="default" v-model="state.form.WebListens" placeholder="短链接端口">
                                    <template #append>
                                        <el-tooltip class="box-item" effect="dark" content="短链接端口列表，多个英文逗号间隔" placement="top">
                                            <el-icon>
                                                <Warning />
                                            </el-icon>
                                        </el-tooltip>
                                    </template>
                                </el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item label="" label-width="0">
                    <el-row>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="端口开始" prop="min">
                                <el-input size="default" v-model="state.form.min" placeholder="长连接端口开始"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
                            <el-form-item label="端口结束" prop="max">
                                <el-input size="default" v-model="state.form.max" placeholder="长连接端口结束"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form-item>
                <el-form-item label="" label-width="0">
                    你可以设置一个端口范围，节点只能添加此范围内的长连接端口监听
                </el-form-item>
                <el-form-item label-width="0">
                    <div class="t-c w-100">
                        <el-button type="primary" :loading="state.loading" @click="submit">确 定</el-button>
                    </div>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import { ref, reactive } from '@vue/reactivity';
import { getConfigure, saveConfigure } from '../../../../apis/configure'
import { shareData } from '../../../../states/shareData'
import { onMounted } from '@vue/runtime-core';
import plugin from './plugin'
import { ElMessage } from 'element-plus';
export default {
    plugin: plugin,
    setup() {
        const formDom = ref(null);
        const state = reactive({
            loading: false,
            form: {
                ConnectEnable: true,
                WebListens: "",
                BufferSize: 3,
                min: 1025,
                max: 65535
            },
            rules: {
                min: [
                    { required: true, message: '必填', trigger: 'blur' },
                    {
                        type: 'number', min: 1025, max: 65535, message: '数字 1025-65535', trigger: 'blur', transform(value) {
                            return Number(value)
                        }
                    }
                ],
                max: [
                    { required: true, message: '必填', trigger: 'blur' },
                    {
                        type: 'number', min: 1025, max: 65535, message: '数字 1025-65535', trigger: 'blur', transform(value) {
                            return Number(value)
                        }
                    }
                ]
            }
        });

        const loadConfig = () => {
            return getConfigure(plugin.config);
        }
        const submit = () => {
            return new Promise((resolve, reject) => {
                formDom.value.validate((valid) => {
                    if (valid == false) {
                        reject();
                        return false;
                    }
                    loadConfig().then((json) => {
                        json.ConnectEnable = state.form.ConnectEnable;
                        json.BufferSize = +state.form.BufferSize;
                        json.WebListens = state.form.WebListens.split(',').filter(c => c.length > 0).map(c => +c);
                        json.TunnelListenRange.Min = +state.form.min;
                        json.TunnelListenRange.Max = +state.form.max;
                        state.loading = true;
                        saveConfigure(plugin.config, JSON.stringify(json)).then((res) => {
                            state.loading = false;
                            resolve();
                            if (res) {
                                ElMessage.success('操作成功!');
                            } else {
                                ElMessage.error('操作失败!');
                            }
                        }).catch(() => {
                            state.loading = false;
                            resolve();
                        });
                    }).catch(reject);
                });
            })
        }

        onMounted(() => {
            loadConfig().then((json) => {
                state.form.ConnectEnable = json.ConnectEnable;
                state.form.BufferSize = json.BufferSize;
                state.form.WebListens = json.WebListens.join(',');
                state.form.min = json.TunnelListenRange.Min;
                state.form.max = json.TunnelListenRange.Max;
            });
        });

        return {
            shareData, state, formDom, submit
        }
    }
}
</script>

<style lang="stylus" scoped>
.el-row {
    width: 100%;
}

.el-form-item:last-child {
    margin-bottom: 0;
}

.inner {
    padding: 2rem;
}

@media screen and (max-width: 768px) {
    .el-col {
        margin-top: 0.6rem;
    }
}
</style>